package mybatis;

import mybatis.dao.StudentDao;
import mybatis.domain.Student;
import mybatis.service.StudentService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * @Author 肖地洋
 * @Date 2022/5/19 21:45
 */
public class test {
    /**
     * 没有导入jdbc依赖报错：
     * 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
     * PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy
     */
    @Test
    public  void  test01(){
        String config = "applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //mybatis之前的用方法
        SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) ctx.getBean("sqlSessionFactory");
        SqlSession sqlSession  = sqlSessionFactory.openSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        List<Student> list = studentDao.selectStudents();
        for (Student student:list){
            System.out.println(student.getName());
        }
        System.out.println("===================dao使用===============");
        // spring集成mybatis之后
        StudentDao studentDao2 = (StudentDao) ctx.getBean("studentDao");
        List<Student> list2 = studentDao2.selectStudents();
        for (Student student:list2){
            System.out.println(student.getName());
        }
        System.out.println("==================service使用===============");
        StudentService service = (StudentService) ctx.getBean("studentService");
        List<Student> list3 =  service.queryStudents();
        for (Student student:list3){
            System.out.println(student.getName());
        }
    }
}
